package com.dongdongshop.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.constant.CookieConstant;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartService;
import com.dongdongshop.util.CookieUtils;
import com.dongdongshop.vo.Cart;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("cartController")
public class CartController {

    @DubboReference
    private CartService cartService;

    /*未登录,添加购物车*/
    @RequestMapping("addCart")                                                      //商品sku编号      商品价格
    public String addCart(HttpServletRequest request, HttpServletResponse response, Long skuId,Integer num){
        addCartPage(request,response,skuId,num);
        return "success-cart";
    }
    @RequestMapping("add")                                            //商品sku编号      商品价格
    public void addCartPage(HttpServletRequest request, HttpServletResponse response, Long skuId,Integer num){
        /*先从cookie中获取原有数据*/
        List<Cart> cartList = getCartList(request,response);
        /*调用Cart服务,追加数据*/
        cartList = cartService.addCart(cartList,skuId,num);
        //===============================已登录===================
        Subject subject = SecurityUtils.getSubject();
        TbUser tbUser = (TbUser)subject.getPrincipal();//获取登录的账号密码
        if (tbUser != null){
            cartService.addCartListRedis(cartList,tbUser.getId());
        }else {
            //===============================未登录===================
            /*将新的数据,添加到cookie中*/            //CookieConstant.CARTCOOKIELLIST:cookie的属性名称-//cookie的value值 //指定编码格式true表示UTF-8
            CookieUtils.setCookie(request,response,CookieConstant.CARTCOOKIELLIST,JSONObject.toJSONString(cartList),true);
        }
    }

    @RequestMapping("getCartList")
    @ResponseBody
    public List<Cart> getCartList(HttpServletRequest request,HttpServletResponse response){
        /*先从cookie中获取原有数据*/                                                             //指定编码格式true表示UTF-8
        String cartList = CookieUtils.getCookieValue(request, CookieConstant.CARTCOOKIELLIST,true);
        if (StrUtil.isBlank(cartList)){//cartList为空
            /*cookie中没有数据*/
            cartList = "[]";
        }
        /*将cookie中的值转为List*/
        List<Cart> carts = JSONObject.parseArray(cartList, Cart.class);

        Subject subject = SecurityUtils.getSubject();
        TbUser tbUser = (TbUser)subject.getPrincipal();//获取登录的账号密码
        if (tbUser!=null){
            //===============================已登录===================
            //更具登录的账号Id前往redis中查询这个账号所关联的购物车
            List<Cart> cartListRedis = cartService.selectCartListRedis(tbUser.getId());
            /*当用户登录之后,将cookie中的数据和redis中是数据合并,
             *然后删除购物车,并返回合并后的数据*/
            if (carts.size() >0){//判断cookie中是否有数据
                /*合并*/
               List<Cart> mergeCartList = cartService.mergeCartList(carts,cartListRedis);
               /*删除cookie中的数据*/
                CookieUtils.deleteCookie(request,response,CookieConstant.CARTCOOKIELLIST);
                /*将合并后的数据存入redis*/
                cartService.addCartListRedis(mergeCartList,tbUser.getId());
                /*返回合并后的数据*/
                return mergeCartList;
            }
            //如果cookie中没有数据,直接返回redis中之前添加的数据
            return cartListRedis;
        }else {//如过用户没有登录,只能获取到cookie中的数据
            //===============================未登录===================
            return carts;
        }

    }
    /*跳转购物车*/
    @RequestMapping("runCartPage")
    public String runCartPage(){
        return "cart";
    }

    /*跳转结算*/
    @RequestMapping("getOrderInfo")
    public String getOrderInfo(){
        return "getOrderInfo";
    }

}
